package Admin;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dushibao.admin.AdminApplication;
import com.dushibao.admin.model.SysConfig;
import com.dushibao.admin.service.ISysConfigService;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  系统配置 测试类
 *
 * @author zqd
 * @date 2021-12-08 11:16:17
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AdminApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SysConfigServiceDemo {

    @Autowired
    ISysConfigService sysConfigService;

    @Test
    public void getMap(){

        Map<String, Object> map = new HashMap<>();

        List<SysConfig> sysConfigs = this.sysConfigService.list();

        sysConfigs.forEach(e->{
            map.put(e.getSysKey(),e.getSysValue());
        });

        System.out.println(map);

    }

    @Test
    public void getById(){
        SysConfig sysConfig = this.sysConfigService.getById(1);
        System.out.println(JSON.toJSONString(sysConfig));
        Assert.assertNotNull(sysConfig);
    }

    @Test
    public void list(){
        List<SysConfig> sysConfigs = this.sysConfigService.list();
        sysConfigs.forEach(System.out::println);
    }

    @Test
    public void save(){
        SysConfig sysConfig = new SysConfig();
        sysConfig.setSysKey("KKKKKKKK");
        sysConfig.setSysValue("VVVVVVVVVV");
        sysConfig.setAddTime(new Date());
        for(int i=0;i<100;i++){
            this.sysConfigService.save(sysConfig);
        }
    }

    @Test
    public void removeById(){
        boolean ret = this.sysConfigService.removeById(1L);
        Assert.assertEquals(true,ret);
    }

    @Test
    public void saveOrUpdate(){
        SysConfig sysConfig = this.sysConfigService.getById(1);
        boolean ret = this.sysConfigService.saveOrUpdate(sysConfig);
        Assert.assertEquals(true,ret);
    }

    @Test
    public void page(){
        Integer currentPage = 1;
        Integer pageSize = 20;

        QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<>();
        IPage<SysConfig> iPage = new Page<>(currentPage,pageSize);
        IPage<SysConfig> page = this.sysConfigService.page(iPage,queryWrapper);

    }

}
